package com.behelpful.demo.search.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.behelpful.common.core.result.R;
import com.behelpful.demo.search.entity.TestEsInfo;
import com.behelpful.demo.search.model.test.TestParam;
import com.behelpful.demo.search.service.test.TestSearchService;
import lombok.AllArgsConstructor;
import org.apache.commons.io.FileUtils;
import org.apache.ibatis.io.Resources;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author Alay
 * @date 2021-05-13 14:04
 */
@RestController
@AllArgsConstructor
@RequestMapping(value = "/test")
public class SearchTestController {

    @Autowired
    private TestSearchService testSearchService;

    @PutMapping(value = "/checkIndex")
    public R checkIndex(String indexName) throws IOException {
        // 索引是否存在
        Boolean existed = testSearchService.existsIndex(indexName);
        if (!existed) {
            // 索引不存在则创建,事先必须先编写 ES 索引 mapping JSON文件,并以索引命名
            File template = Resources.getResourceAsFile("indices/" + indexName + ".json");
            String jsonStr = FileUtils.readFileToString(template);
            Boolean created = testSearchService.createIndex(indexName, jsonStr);
            return R.ok(created);
        }
        return R.ok(true);
    }


    @PostMapping(value = "/add")
    public R add(@RequestBody TestEsInfo testEsInfo) {
        Boolean isOk = testSearchService.saveEntity(testEsInfo);
        return R.ok(isOk);
    }

    @DeleteMapping(value = "/delById/{id}")
    public R delById(@PathVariable("id") String id) {
        Boolean isDeleted = testSearchService.deleteById(id);
        return R.ok(isDeleted);
    }

    @PutMapping(value = "/update")
    public R update(@RequestBody TestEsInfo testEsInfo) {
        Boolean isOk = testSearchService.updateById(testEsInfo);
        return R.ok(isOk);
    }

    @PutMapping(value = "/getById/{id}")
    public R getById(@PathVariable("id") String id) {
        TestEsInfo testEsInfo = testSearchService.selectById(id);
        return R.ok(testEsInfo);
    }


    @GetMapping(value = "/select")
    public R select(Page page, TestParam params) {
        long size = page.getSize();
        long from = (page.getCurrent() - 1) * size;

        // 自定义排序规则测试
        List<OrderItem> orders = Arrays.asList(new OrderItem("field_1", false), new OrderItem("field_2", false));

        SearchSourceBuilder searchBuilder = testSearchService.searchBuilder((int) from, (int) size, orders, params);
        page = testSearchService.pageQuery(page, searchBuilder);
        if (page.getRecords().size() > 0) {
            // 数据返回前的处理
            testSearchService.voHandler(page.getRecords(), params);
        }
        return R.ok(page);
    }

}